﻿/******************************************************************************************************************************
 * Form that allows you to add a recipe to be stored by the RecipeBox application
 * Author: Morgan Davis
 * Northwestern Michigan College (student assignment)
 * Created: 27 November 2013
 * ****************************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecipeBox
{
    public partial class frmAddRecipe : Form
    {
        public frmAddRecipe()
        {
            InitializeComponent();
        }

        private void btnAddInstruction_Click(object sender, EventArgs e) //adds textbox as a line in the instructions listbox
        {
            if (txtAddInstruction.Text != "")
            {
                lstInstruction.Items.Add(txtAddInstruction.Text.Trim());
                txtAddInstruction.Clear();
                txtAddInstruction.Focus();
            }
        }

        private void btnAddIngredient_Click(object sender, EventArgs e)//adds textbox as a line in the ingredients listbox
        {
            if (txtAddIngredient.Text != "")
            {
                lstIngredient.Items.Add(txtAddIngredient.Text.Trim());
                txtAddIngredient.Clear();
                txtAddIngredient.Focus();
            }
        }

        private void btnRemInstruction_Click(object sender, EventArgs e)//removes selected item from instruction listbox
        {
            if (lstInstruction.SelectedIndex > 0)
            {
                lstInstruction.Items.RemoveAt(lstInstruction.SelectedIndex);
            }
        }

        private void btnRemIngredient_Click(object sender, EventArgs e)//removes selected item from ingredients listbox
        {
            if (lstIngredient.SelectedIndex > 0)
            {
                lstIngredient.Items.RemoveAt(lstIngredient.SelectedIndex);
            }
        }

        //Combines instruction/ingredient listboxes into a List<String> then creates a new Recipe object to be passed
        //to the CreateRecipe method in the RecipeXML class
        private void btnSubmit_Click(object sender, EventArgs e)
        {

            if (txtName.Text != "" && txtCredited.Text != "" && lstInstruction.Items.Count > 0 && lstIngredient.Items.Count > 0)
            {
                DialogResult myResult = MessageBox.Show("Are you sure you want add this recipe to your collection as it is?", "Verify Recipe Creation", MessageBoxButtons.YesNo);
                if (myResult == DialogResult.Yes)
                {
                    List<String> ingredient = new List<String>();
                    foreach (string item in lstIngredient.Items) //combines listbox into list of strings
                    {
                        ingredient.Add(item);
                    }
                    List<String> instruction = new List<String>();

                    foreach (string item in lstInstruction.Items)//combines listbox into list of strings
                    {
                        instruction.Add(item);
                    }

                    Recipe newRecipe = new Recipe() //declare/instantiate new Recipe object to passed to RecipeXML class
                    {
                        name = txtName.Text.Trim(),
                        creditedCook = txtCredited.Text.Trim(),
                        ingredients = ingredient,
                        instructions = instruction
                    };
                    RecipeXML.CreateRecipe(newRecipe); //pass to CreateRecipe method to be added to the xml file
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
                MessageBox.Show("You must provide a name, the credited cook/chef, and at least one instruction and ingredient.", "Don't be ridiculous.");
            }
        }
    }
}
